package com.zidian.teacher.activity;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import com.google.gson.Gson;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;
import com.zidian.teacher.R;
import com.zidian.teacher.adapter.EvaluateOtherListAdapter;
import com.zidian.teacher.bean.EvaluateOtherListBean;
import com.zidian.teacher.common.Constant;
import com.zidian.teacher.common.ToolBarActivity;
import com.zidian.teacher.utils.GetUserInfo;
import com.zidian.teacher.utils.ToastUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import okhttp3.Call;

/**
 * Created by Administrator on 2016/9/5 0005.
 */
public class EvaluateOtherListActivity extends ToolBarActivity {


    public SwipeRefreshLayout mSwipeRefreshWidget;
    private RecyclerView mRecyclerView;


    boolean isLoading;
    private List<EvaluateOtherListBean.DataBean> data = new ArrayList<>();
    private EvaluateOtherListAdapter adapter;
    private Handler handler = new Handler();
    //RecyclerView是否在刷新
    private boolean isRefreshing = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setToolbarView(R.layout.toolbar_left);
        setContentView(R.layout.activity_evaluate_other_list);
        setTitle("同行评价");


        adapter = new EvaluateOtherListAdapter(this, data);
        mSwipeRefreshWidget = (SwipeRefreshLayout) findViewById(R.id.swipe_refresh_widget);
        mRecyclerView = (RecyclerView) findViewById(R.id.list);
        initView();
        initData();
    }

    public void initView() {
        mSwipeRefreshWidget.setColorSchemeResources(R.color.green);

        mSwipeRefreshWidget.post(new Runnable() {
            @Override
            public void run() {
                mSwipeRefreshWidget.setRefreshing(true);
                isRefreshing = true;
            }
        });
        mSwipeRefreshWidget.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        data.clear();
                        isRefreshing = true;
                        getEvaluateList();
                    }
                }, 2000);
            }
        });
        mRecyclerView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (isRefreshing) {
                    return true;
                }else {
                    return false;
                }
            }
        });


        final LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.setAdapter(adapter);
        mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                Log.d("test", "StateChanged = " + newState);


            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                Log.d("test", "onScrolled");

                int lastVisibleItemPosition = layoutManager.findLastVisibleItemPosition();
                if (lastVisibleItemPosition + 1 == adapter.getItemCount()) {
                    Log.d("test", "loading executed");

                    boolean isRefreshing = mSwipeRefreshWidget.isRefreshing();
                    if (isRefreshing) {
                        adapter.notifyItemRemoved(adapter.getItemCount());
                        return;
                    }
                }
            }
        });


    }

    public void initData() {
        getEvaluateList();
    }


    private void getEvaluateList() {

        String loginUrl = Constant.URL_TEACHER + "ToEvaluateOthers/SelectSameGroup";
        HashMap<String, String> map = new HashMap<>();
        map.put("teacherId", GetUserInfo.getTeacherId(context));
        map.put("token", GetUserInfo.getToken(context));


        OkHttpUtils.post().url(loginUrl).params(map).build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                Log.e("E", e.toString());
                ToastUtil.showToast(context, "获取列表失败！");
                isRefreshing =false;
            }

            @Override
            public void onResponse(String response, int id) {
                Log.e("T", response);
                mSwipeRefreshWidget.setRefreshing(false);
                isRefreshing = false;
                try {
                    JSONObject jsonObject = null;
                    jsonObject = new JSONObject(response);
                    int resultCode = jsonObject.getInt("code");
                    if (resultCode != 200) {
                        ToastUtil.showToast(context, jsonObject.getString("message"));
                        return;
                    } else {
                    }

                } catch (JSONException e) {

                }
//
                Gson gson = new Gson();
                EvaluateOtherListBean model = gson.fromJson(response, EvaluateOtherListBean.class);
                if (model.isSuccess()) {
                    data.addAll(model.getData());

                    adapter.notifyDataSetChanged();

                }

            }
        });
    }


}
